package org.example.one.controller;


import org.example.one.common.Student;
import org.example.one.domain.Test;
import org.example.one.service.StudentService;
import org.example.one.service.TestService;
import org.example.one.service.ThemeService;
import org.example.one.common.Result;
import org.example.one.domain.Theme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/theme")
public class ThemeController {
    @Autowired
    ThemeService themeService;
    @Autowired
    StudentService studentService;
    @Autowired
    TestService testService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @PostMapping("/AddTheme")
    public Result addtheme(@RequestBody Theme theme, HttpServletRequest request) {
        String token= request.getHeader("token");
        Student student= (Student) redisTemplate.opsForValue().get(token);
        if (student == null) {
            return Result.fail(404,"未登录",null);
        }else if(student.getSid()!=114514){
            return Result.fail(401,"没有权限",null);
        }
        theme.setDate(Calendar.getInstance().getTime());
        System.out.println(theme);

        themeService.addtheme(theme);
        return Result.succ(200,"添加成功",theme);
    }
    @GetMapping("/GetTheme")
    public Result gettheme(HttpServletRequest request) {
      Set<Theme> set=new HashSet<>();
        themeService.getTheme(set);
        return Result.succ(200,"查询成功",new ArrayList<Theme>(set));
    }
    @GetMapping("/getthemeall")
    public Result getthemelist(HttpServletRequest request) {
        Set<Theme> set=new HashSet<>();
        themeService.getThemeall(set);
        return Result.succ(200,"操作成功",new ArrayList<Theme>(set));
    }
    @DeleteMapping("/deleteTheme/{tid}")
    public Result deletetheme(@PathVariable Integer tid, HttpServletRequest request) {
        System.out.println(tid);
        String token= request.getHeader("token");

        Student student= (Student) redisTemplate.opsForValue().get(token);
        if (student.getSid()!=114514) {
            return Result.fail(401,"没有权限",null);
        }
        themeService.removeById(tid);
        return Result.succ(200,"删除成功",null);
    }
    @PostMapping("/addrecord")
    public Result addrecord(@RequestBody Test test, HttpServletRequest request) {
        String token= request.getHeader("token");
        Student student= (Student) redisTemplate.opsForValue().get(token);
        if(student==null){
            return  Result.fail(404,"未登录",null);
        }
        test.setTime( Calendar.getInstance().getTime());
        testService.save(test);
        return Result.succ(200,"添加成功",test);
    }
    @GetMapping("/getmbti")
    public Result getmbti(HttpServletRequest request) {
        String token= request.getHeader("token");
        System.out.println(token);
        Student student= (Student) redisTemplate.opsForValue().get(token);
        if (student == null) {
            return Result.fail(404,"未登录",null);
        }
        List<Test> test=testService.getNewmbtiByid(student.getSid());
        return Result.succ(200,"请求成功",test);
    }

    @GetMapping("/getmbticlass")
    public Result getmbtclass(HttpServletRequest request) {
        String token= request.getHeader("token");
        Student student= (Student) redisTemplate.opsForValue().get(token);
        if (student == null) {
            return Result.fail(404,"未登录",null);
        }
        System.out.println(student);
        return Result.succ(200,"请求成功",testService.getNewmbtiByclass(student));
    }

}
